package org.chs.ts.batch.threadservice;


import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.chs.ts.base.batch.vo.entity.BatchDailyExecReq;
import org.chs.ts.base.batch.vo.entity.BatchDailyExecResp;
import org.chs.ts.base.constant.CommonConstant;
import org.chs.ts.batch.util.ConvertToUtil;
import org.chs.ts.batch.exception.TaskDailyException;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.Callable;

/**
 * @Compay 盛唐天行
 * @Author: wuwei
 * @Censor By wuwei
 * @Sence 2023/1/9 14:11
 * @Modify 2023/1/9 14:11
 * @Description
 */
@Getter
@Setter
@Slf4j
public abstract class BatchDailyServiceBase<E, R> implements Callable<BatchDailyExecResp> {


    private long taskStartTime;

    private long taskEndTime;


    private long taskDailyStepStartTime;


    private long taskDailyStepEndTime;

    private String path;

    private String ip;

    private String port;



    private E taskPublishObject;


    private R taskDailyStepObject;

    private BatchDailyExecReq request;



    /**
     * 异步执行类
     */
    public BatchDailyExecResp doProcess(BatchDailyExecReq request) {
        BatchDailyExecResp obj = new BatchDailyExecResp();
        obj.setRtnCode(CommonConstant.RTN_CODE_SUCCESS);

        log.info("线程调用参数：" + ConvertToUtil.printObjectInfo(request, new ArrayList<>()));
        return obj;
    }

    public String getRunningTime() {
        String startTime = ConvertToUtil.getRunningTime();
        this.taskStartTime = Long.parseLong(startTime);
        return startTime;
    }

    public String getRunningTime(String startTime) {
        String endTime = ConvertToUtil.getRunningTime(startTime);
        this.taskEndTime = Long.parseLong(ConvertToUtil.getRunningTime());
        return endTime;
    }

    public String getStepRunningTime() {
        String startTime = ConvertToUtil.getRunningTime();
        this.taskDailyStepStartTime = Long.parseLong(startTime);
        return startTime;
    }

    public String getStepRunningTime(String startTime) {
        String endTime = ConvertToUtil.getRunningTime(startTime);
        this.taskDailyStepEndTime = Long.parseLong(ConvertToUtil.getRunningTime());
        return endTime;
    }

    public abstract BatchDailyExecResp execute(BatchDailyExecReq request) throws TaskDailyException;

    @Override
    public BatchDailyExecResp call() {
        return null;
    }



    /**
     * 回写子任务 执行结果
     */
    public abstract BatchDailyExecResp writeBackResult(BatchDailyExecResp request,R taskStepObject);


    public abstract void writeBackResultForRedis(R taskStepObject);


    /**
     * 初始化 参数
     */
    public void initParamter(E taskExec, R taskStep, String ip, String port, String path) {
        this.setTaskPublishObject(taskExec);
        this.setTaskDailyStepObject(taskStep);
        this.setIp(ip);
        this.setPort(port);
        this.setPath(path);
    }

    /**
     * 打印运行信息(头部)
     *
     * @param className 执行类名称
     * @param request   请求对象
     */
    protected final String pringRunningInfo(String className, BatchDailyExecReq request) {
        //String defaultMsg = "BatchServiceBase -> pringRunningInfo exception!";
        this.request = request;
        return (className + " -> execute running   " + "跑批任务对象：" + ConvertToUtil.printObjectInfo(request, new ArrayList<>(Arrays.asList(
                        "taskGroup",
                        "taskId",
                        "taskExecId",
                        "execOrder",
                        "connectTimeout",
                        "executeTimeout",
                        "taskDate",
                        "sliceServiceClass"
                ))

        ) + " " + "--------------------------------------------------------");
    }

    /**
     * 打印运行信息(底部)
     *
     * @param className   执行类名称
     * @param request     请求对象
     * @param consumeTime 消耗时间
     */
    protected final String pringRunningInfo(String className, BatchDailyExecReq request, String consumeTime) {

        return (className + " -> execute 耗时:" + (consumeTime) + ConvertToUtil.printObjectInfo(request, new ArrayList<>()) + " --------------------------------------------------------");

    }

    /**
     * 打印子任务运行信息(头部)
     *
     * @param className    执行类名称
     * @param taskStepExec 请求对象
     */
    protected final String pringRunningInfo(String className, R taskStepExec) {
        //String defaultMsg = "BatchServiceBase -> pringRunningInfo exception!";

        return (className + " -> execute running   " + "跑批子任务对象：" + ConvertToUtil.printObjectInfo(taskStepExec, new ArrayList<>()) + " " + " --------------------------------------------------------");
    }

    /**
     * 打印运行信息(底部)
     *
     * @param className    执行类名称
     * @param taskStepExec 请求对象
     * @param consumeTime  消耗时间
     */
    protected final String pringRunningInfo(String className, R taskStepExec, String consumeTime) {

        return (className + " -> 子任务 execute 耗时:" + (consumeTime) + ConvertToUtil.printObjectInfo(taskStepExec, new ArrayList<>()) + " --------------------------------------------------------");

    }


}
